package com.cchr.acms.controller;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.codehaus.groovy.tools.shell.IO.Verbosity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.cchr.acms.constant.CaseStatus;
import com.cchr.acms.constant.IKeyValue;
import com.cchr.acms.model.AbuseType;
import com.cchr.acms.model.AbusedPerson;
import com.cchr.acms.model.Action;
import com.cchr.acms.model.Attorney;
import com.cchr.acms.model.CaseFollowUp;
import com.cchr.acms.model.CaseInfo;
import com.cchr.acms.model.DoctorInfo;
import com.cchr.acms.model.Drug;
import com.cchr.acms.model.FacilityInfo;
import com.cchr.acms.model.ReportingPerson;
import com.cchr.acms.service.AbuseTypeService;
import com.cchr.acms.service.AbusedPersonService;
import com.cchr.acms.service.ActionService;
import com.cchr.acms.service.AttorneyService;
import com.cchr.acms.service.CaseFollowUpService;
import com.cchr.acms.service.CaseInfoService;
import com.cchr.acms.service.CaseService;
import com.cchr.acms.service.DoctorInfoService;
import com.cchr.acms.service.DrugService;
import com.cchr.acms.service.FacilityInfoService;
import com.cchr.acms.service.ReportingPersonService;
import com.cchr.acms.util.ActionPdfVO;
import com.cchr.acms.util.DateUtil;
import com.cchr.acms.util.DoctorInfoPdfVO;
import com.cchr.acms.util.FacilitiesPdfVO;
import com.cchr.acms.util.FollowUpPdfVO;
import com.cchr.acms.util.PersonInfoPdfVO;
import com.cchr.acms.util.StringUtil;
import com.google.common.collect.Lists;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

@Controller
@RequestMapping("/rpt")
public class ReportController {

	@Autowired
	private CaseInfoService caseInfoService;
	@Autowired
	private AbusedPersonService abusedPersonService;
	@Autowired
	private AbuseTypeService abuseTypeService;
	@Autowired
	private FacilityInfoService facilityInfoService;
	@Autowired
	private DoctorInfoService doctorInfoService;
	@Autowired
	private ReportingPersonService reportingPersonService;
	@Autowired
	private DrugService drugService;
	@Autowired
	private AttorneyService attorneyService;
	@Autowired
	private ActionService actionService;
	@Autowired
	private CaseFollowUpService caseFollowUpService;
		
	@RequestMapping(value = "/person/{id}", method = RequestMethod.GET)
	public String personRpt(@PathVariable Integer id,Model model) {
		
		PersonInfoPdfVO personInfo=getPersonInfo(id);
		List<PersonInfoPdfVO> personList=Lists.newArrayList();
		personList.add(personInfo);
		
		// 报表数据源
		JRDataSource mainDataSource = new JRBeanCollectionDataSource(personList);
		JRDataSource facilitiesDataSource = new JRBeanCollectionDataSource(getFacilityInfos(id));
		JRDataSource doctorDataSource = new JRBeanCollectionDataSource(getDoctorInfos(id));
		
		List<ActionPdfVO> actionList= Lists.newArrayList();
		if(personInfo!=null){
			actionList.addAll(personInfo.getActionNames());
		}
		JRDataSource actionDataSource = new JRBeanCollectionDataSource(actionList);
		
		// 动态指定报表模板url
		model.addAttribute("url", "/WEB-INF/jasper/personRpt/PersonInfoReport.jrxml");
		model.addAttribute("format", "pdf"); // 报表格式
		model.addAttribute("mainDataSource", mainDataSource);
		model.addAttribute("FacilitiesRptDatasource", facilitiesDataSource);
		model.addAttribute("DoctorRptDatasource", doctorDataSource);
		model.addAttribute("ActionRptDatasource", actionDataSource);
		
		return "personRptView"; 
	}
	
	@RequestMapping(value = "/followUp/{id}", method = RequestMethod.GET)
	public String followRpt(@PathVariable Integer id,Model model) {
		
		FollowUpPdfVO followUpPdfVO=getFollowUpInfo(id);
		List<FollowUpPdfVO> followUpList=Lists.newArrayList();
		followUpList.add(followUpPdfVO);
		
		// 报表数据源
		JRDataSource mainDataSource = new JRBeanCollectionDataSource(followUpList);
		
		// 动态指定报表模板url
		model.addAttribute("url", "/WEB-INF/jasper/followUpRpt/FollowUpReport.jrxml");
		model.addAttribute("format", "pdf"); // 报表格式
		model.addAttribute("mainDataSource", mainDataSource);
		
		return "followUpRptView"; 
	}
		
	protected PersonInfoPdfVO getPersonInfo(Integer id){
		PersonInfoPdfVO vo= new PersonInfoPdfVO();
				
		//Set caseInfo
		CaseInfo caseInfo = caseInfoService.getCaseInfoById(id);
		if(caseInfo!=null){
			//Set abusedPerson
			setAbusedPerson(id, vo);
			//Set ReportPerson
			setReportingPerson(id, vo);
			//Set Attoney
			setAttoney(id,vo);
			// Set abuse Type
			setAbuseType(caseInfo.getTypeIds(),vo);
						
			vo.setCaseNo(caseInfo.getCaseNo());	
			vo.setInitalContact(DateUtil.dateTOString(caseInfo.getCreateDate(), DateUtil.MM_DD_YYYY));
			vo.setActionNames(getActionNames(caseInfo.getActionIds()));
			vo.setOtherActions("• "+caseInfo.getOtherAction());
			
			StringBuilder abuseDate = new StringBuilder();
			if(caseInfo.getCaseStart()!=null){
				abuseDate.append(DateUtil.dateTOString(caseInfo.getCaseStart(), DateUtil.MM_DD_YYYY));
				abuseDate.append("-");
			}
			abuseDate.append(DateUtil.dateTOString(caseInfo.getCaseEnd(), DateUtil.MM_DD_YYYY));
			vo.setAbuseDate(abuseDate.toString());
			vo.setAbuseSummary(caseInfo.getSummary());
			
			vo.setUseInsurance(caseInfo.getUserInsurance());
			if("1".equalsIgnoreCase(caseInfo.getUserInsurance())){
				vo.setInsuranceType("What Type Was Used? "+caseInfo.getInsuranceType());
				vo.setInsurance1Company(caseInfo.getInsurance1CompanyName());
				vo.setNoteDetail(caseInfo.getNotesOnInsuranceDetail());
				vo.setNoteDetailLabel("Notes on Insurance Details:");
			}
			
			vo.setDrugsPrescribed(caseInfo.getDrugsPrescribed());
			if("1".equalsIgnoreCase(caseInfo.getDrugsPrescribed())){
				setDrugName(caseInfo.getDrugIds(),vo);
				vo.setDrugTakeTime("Drugs to taken for: "+caseInfo.getDrugsTakeTime());
				vo.setDrugDosage("Drugs dosage: "+caseInfo.getDrugsDosage());
				if("1".equalsIgnoreCase(caseInfo.getStillTakeDrugs())){
					vo.setDrugStillTaking("Still Taking Psychiatric Drugs? YES");	
				}else{
					vo.setDrugStillTaking("Still Taking Psychiatric Drugs? NO");	
				}
			}
			
			if(StringUtils.isNotBlank(caseInfo.getBestTimeToContact())){
				vo.setContactTime("Best Time Contact:  "+caseInfo.getBestTimeToContact());
			}
			if(StringUtils.isNotBlank(caseInfo.getBestWayToContact())){
				vo.setContactWay("Best Way to Contact:  "+caseInfo.getBestWayToContact());
			}
		}
		
		return vo;
	}
	
	protected FollowUpPdfVO getFollowUpInfo(Integer id){
		FollowUpPdfVO vo = new FollowUpPdfVO();

		CaseInfo caseInfo = caseInfoService.getCaseInfoById(id);
		if (caseInfo != null) {
			vo.setCaseNo(caseInfo.getCaseNo());	
			vo.setInitalContact(DateUtil.dateTOString(caseInfo.getCreateDate(), DateUtil.MM_DD_YYYY));
			
			AbusedPerson abusedPerson = abusedPersonService.getAbusedPersonByCaseId(id);
			
			StringBuilder sb= new StringBuilder();
			sb.append(abusedPerson.getFirstName()).append(" ");
			sb.append(abusedPerson.getMiddleName()).append(" ");
			sb.append(abusedPerson.getLastName());
			
			vo.setName(sb.toString());
			
			CaseFollowUp followup = caseFollowUpService.getCaseFollowUpByCaseId(id);
			if(followup!=null){		
						
				setContactInfo(followup,vo);
				setAttorneyStatus(followup,vo);
				setAbuseCase(followup,vo);
				setComplaintInfo(followup,vo);
				setResponseInfo(followup,vo);
				setSigneInfo(followup,vo);
				setReceivedDoc(followup,vo);
				
				vo.setOtherActionsTaken(followup.getOtherActionsTaken());
				vo.setCurrentStatusOnCase(followup.getCurrentStatusOnCase());
				
				String updateDate=DateUtil.dateTOString(followup.getCaseLastUpdatedDate(), DateUtil.MM_DD_YYYY);
				vo.setCaseLastUpdatedDate(updateDate);

				vo.setCaseCategory(followup.getCaseCategory());			
			}
			
			IKeyValue[] values=CaseStatus.getList();
			if(values!=null){
				for(IKeyValue value:values){
					if(caseInfo.getStatus().equalsIgnoreCase(value.getValue())){
						vo.setCaseStatusKey(value.getkey());
					}
				}
			}

		}
		
		return vo;
	}
	
	protected List<FacilitiesPdfVO> getFacilityInfos(int id){
		List<FacilitiesPdfVO> list= Lists.newArrayList();
		List<FacilityInfo> facilityInfos = facilityInfoService.getFacilityInfoByCaseId(id);
		if(facilityInfos!=null){
			for(int i=0;i<facilityInfos.size();i++){
				FacilityInfo info= facilityInfos.get(i);
				if(info==null){
					continue;
				}
				FacilitiesPdfVO vo= new FacilitiesPdfVO();
				vo.setFacilityNo("Facility "+(i+1)+":");
				if(StringUtils.isBlank(info.getName())){
					vo.setFacilityName("N/A");
				}else{
					String name=info.getName()+",";
					if(info.getType()!=null && "other".equalsIgnoreCase(info.getType())){
						name+=info.getTypeOther();
					}else{
						name+=info.getType();
					}
					vo.setFacilityName(name);
					
					StringBuilder sb= new StringBuilder();
					if(StringUtils.isNotBlank(info.getStreetAddress())){
						sb.append(info.getStreetAddress()).append(" ");
					}
					if(StringUtils.isNotBlank(info.getCity())){
						sb.append(info.getCity()).append(" ");
					}
					sb.append(info.getState()).append(" ");
					sb.append(info.getZip());
					vo.setFacilityAddr(sb.toString());
					
					vo.setFacilityCountry(info.getCountry());
					
					if(StringUtils.isNotBlank(info.getTelephone())){
						vo.setFacilityTel("Tel: "+info.getTelephone());
					}
				}
				
				list.add(vo);
			}
		}
		
		return list;
	}
	
	protected List<DoctorInfoPdfVO> getDoctorInfos(int id){
		List<DoctorInfoPdfVO> list= Lists.newArrayList();
		List<DoctorInfo> doctorInfos = doctorInfoService.queryDoctorInfoByCaseId(id);
		if(doctorInfos!=null){
			for(int i=0;i<doctorInfos.size();i++){
				DoctorInfo info=doctorInfos.get(i);
				if(info==null){
					continue;
				}
				DoctorInfoPdfVO vo= new DoctorInfoPdfVO();
				vo.setDoctNo("Doctor "+(i+1)+":");
				if(StringUtils.isBlank(info.getName())){
					vo.setDoctName("N/A");
				}else{
					StringBuilder sb = new StringBuilder();
					if(StringUtils.isNotBlank(info.getName())){
						sb.append(info.getName()).append(" ");
					}
					if(StringUtils.isNotBlank(info.getLastName())){
						sb.append(info.getLastName()).append(",");
					}
					sb.append(info.getDocType());
					vo.setDoctName(sb.toString());
					
					sb=new StringBuilder();
					if(StringUtils.isNotBlank(info.getStreetAddress())){
						sb.append(info.getStreetAddress()).append(",");
					}
					if(StringUtils.isNotBlank(info.getCity())){
						sb.append(info.getCity()).append(",");
					}
					sb.append(info.getState()).append(" ");
					sb.append(info.getZip());
					vo.setDoctAddr(sb.toString());
					
					vo.setDoctCountry(info.getCountry());

					if(StringUtils.isNotBlank(info.getTelephone())){
						vo.setDoctTel("Tel: "+info.getTelephone());
					}
				}
				list.add(vo);
			}
		}
		
		return list;
	}
	
	protected void setReportingPerson(int id,PersonInfoPdfVO vo){
		ReportingPerson reportPerson= reportingPersonService.getReportingPersonByCaseId(id);
		if(reportPerson!=null){
			StringBuilder sb=new StringBuilder();
			if(StringUtils.isNotBlank(reportPerson.getFirstName())||
					StringUtils.isNotBlank(reportPerson.getMiddleName())||
					StringUtils.isNotBlank(reportPerson.getLastName())){
				StringUtil.append(sb, reportPerson.getFirstName(), " ");
				StringUtil.append(sb, reportPerson.getMiddleName(), " ");
				StringUtil.append(sb, reportPerson.getLastName()," - ");
			}
			if(StringUtils.isBlank(reportPerson.getRelation())){
				StringUtil.append(sb, reportPerson.getRelationOther());
			}else{
				sb.append(reportPerson.getRelation());
			}
			vo.setReportName(sb.toString());
			
			sb=new StringBuilder();
			StringUtil.append(sb, reportPerson.getStreetAddress());
			vo.setReportAddr(sb.toString());
			
			if(StringUtils.isNotBlank(reportPerson.getCity())||
					StringUtils.isNotBlank(reportPerson.getState())||
					StringUtils.isNotBlank(reportPerson.getZip())){
				sb=new StringBuilder();
				if(StringUtils.isNotBlank(reportPerson.getCity())){
					sb.append(reportPerson.getCity()).append(", ");
				}
				if(StringUtils.isNotBlank(reportPerson.getState())){
					sb.append(reportPerson.getState()).append(" ");
				}
				StringUtil.append(sb, reportPerson.getZip());
				
				vo.setReportCity(sb.toString());
			}
			
			sb=new StringBuilder();
			StringUtil.append(sb, reportPerson.getCountry());
			vo.setReportCountry(sb.toString());
			
			if(StringUtils.isNotBlank(reportPerson.getTelephone())||
					StringUtils.isNotBlank(reportPerson.getEmail())){
				vo.setReportTel(reportPerson.getTelephone());
				vo.setReportEmail(reportPerson.getEmail());
			}

		}
	}
	
	protected void setAbusedPerson(int id,PersonInfoPdfVO vo){
		AbusedPerson abusedPerson = abusedPersonService.getAbusedPersonByCaseId(id);
			
		//Set abusedPerson
		if(abusedPerson!=null){
//			AbusedPerson person=caseInfo.getAbusedPerson();
			StringBuilder sb= new StringBuilder();
			
			StringUtil.append(sb,abusedPerson.getFirstName(), " " );
			StringUtil.append(sb,abusedPerson.getMiddleName(), " ");
			StringUtil.append(sb,abusedPerson.getLastName());
			
			vo.setName(sb.toString());
			vo.setPersonName(sb.toString());
			
			vo.setStreetAddr(abusedPerson.getStreetAddress());
			
			sb= new StringBuilder();
			StringUtil.append(sb,abusedPerson.getCity(), ", " );
			StringUtil.append(sb,abusedPerson.getState(), " " );
			StringUtil.append(sb,abusedPerson.getZip());
			vo.setCity(sb.toString());
			
			sb= new StringBuilder();
			StringUtil.append(sb,abusedPerson.getCountry());
			vo.setCountry(sb.toString());
			vo.setTelphone(abusedPerson.getTelephone());
			vo.setEmail(abusedPerson.getEmail());
			
			sb= new StringBuilder();
			String birthDate= DateUtil.dateTOString(abusedPerson.getBirthDay(), DateUtil.MM_DD_YYYY);
			if(birthDate!=null){
				sb.append("Birthday: ");
				sb.append(birthDate).append(", ");
			}
			sb.append("approx. ");
			if(abusedPerson.getAge()!=null){
				sb.append(abusedPerson.getAge());
				sb.append(" years old");
			}
			
			vo.setBirthDay(sb.toString());
		}
	}
	
	protected void setDrugName(String drugIds,PersonInfoPdfVO vo){
		if(StringUtils.isNotBlank(drugIds))
		{
			String[] drugIdArr = drugIds.split(",");
			if(drugIdArr!=null && drugIdArr.length > 0)
			{
				List<String> drugIdList = Arrays.asList(drugIdArr);
				List<Drug> drugs = drugService.getAllDrug();
				if(drugs!=null){
					StringBuilder sb = new StringBuilder();
					for(Drug drug:drugs){
						if(drugIdList.contains(String.valueOf(drug.getId()))){
							sb.append(drug.getName()).append(",");
						}		
					}
					if(sb.length()>0 ){
						vo.setDrugNames(sb.substring(0, sb.lastIndexOf(",")));
					}
				}
			}
		}
	}
	
	protected void setAbuseType(String typeIds,PersonInfoPdfVO vo) {
		if(StringUtils.isNotBlank(typeIds)){
			StringBuilder sb = new StringBuilder();
			String[] typeIdArr = typeIds.split(",");
			if(typeIdArr.length > 0)
			{
				List<String> typeIdList = Arrays.asList(typeIdArr);
				List<AbuseType> abuseTypes = abuseTypeService.getAllAbuseType();
		    	for(AbuseType type : abuseTypes)
		    	{
		    		if(typeIdList.contains(String.valueOf(type.getId())))
		    		{
		    			sb.append(type.getName()).append(", ");
		    		}
		    	}
			}
			if(sb.length()>0 ){
				vo.setAbuseType(sb.substring(0, sb.lastIndexOf(",")));
			}
		}
	}
	
	protected void setAttoney(int id, PersonInfoPdfVO vo){
		Attorney attorney = attorneyService.getAttorneyByCaseId(id);
		if(attorney!=null){
			vo.setWorkWithAttorney(attorney.getWorkWithAttorney());
			if("1".equalsIgnoreCase(attorney.getWorkWithAttorney())){
				vo.setAttorneyStaticText("Attorney Info: ");
				if(StringUtils.isNotBlank(attorney.getName())){
					vo.setAttorneyName(attorney.getName());
				}
				
				StringBuilder sb = new StringBuilder(); 
				if(StringUtils.isNotBlank(attorney.getStreetAddress())){
						sb.append(attorney.getStreetAddress()).append(",");
				}
				if(StringUtils.isNotBlank(attorney.getCity())){
					sb.append(attorney.getCity()).append(",");
				}
				if(StringUtils.isNotBlank(attorney.getState())||
						StringUtils.isNotBlank(attorney.getZip())){
					sb.append(attorney.getState()).append(" ").append(attorney.getZip());
				}
				vo.setAttorneyAddr(sb.toString());
				
				sb= new StringBuilder();
				if(StringUtils.isNotBlank(attorney.getTelephone())){
					sb.append(attorney.getTelephone()).append(",");
				}
				sb.append(attorney.getEmail());
				if(sb.length()>0){
					vo.setAttorneyTel(sb.toString());
				}
			}else{
				if("1".equalsIgnoreCase(attorney.getAssistanceAttorney())){
					vo.setAttorneyPrompt1("Would Like Assistance in Getting an Attorney to File Charges or Represent Your Case? ");
					vo.setAttorneyPrompt2("Yes, the person wants assistance finding an attorney.");
				}
			}
		}
	}
	
	protected List<ActionPdfVO> getActionNames(String actionIds){
		List<ActionPdfVO> list= Lists.newArrayList();
		if(StringUtils.isNotBlank(actionIds))
		{
			String[] actionIdArr = actionIds.split(",");
			if(actionIdArr!=null && actionIdArr.length > 0)
			{
				List<String> actionIdList = Arrays.asList(actionIdArr);
				List<Action> actions = actionService.getAllAction();
		    	for(Action action : actions)
		    	{
		    		if(actionIdList.contains(String.valueOf(action.getId())))
		    		{
		    			list.add(new ActionPdfVO("• "+action.getName()));
		    		}
		    	}
			}
		}
		return list;
	}
		
	protected void setAbuseCase(CaseFollowUp followup,FollowUpPdfVO vo){
		StringBuilder sb = new StringBuilder();
//		sb.append("Abuse Case Referred to Attorney? ");
		if("1".equals(followup.getReferAttorneyStatus())){
			sb.append(followup.getReferAttorneyFirstName()).append(" ");
			sb.append(followup.getReferAttorneyLastName());
			vo.setReferAttorneyStatus(sb.toString());
			
			sb = new StringBuilder();
			sb.append("Date Referred: ");
			String date=DateUtil.dateTOString(followup.getReferAttorneyDate(), DateUtil.MM_DD_YYYY);
			if(StringUtils.isNotBlank(date)){
				sb.append(date);
			}
			vo.setReferAttorneyDate(sb.toString());
			
			sb = new StringBuilder();
			sb.append("Accepted by attorney: ");
			if("1".equals(followup.getReferAttorneyAccteptFlag())){
				sb.append("Yes");
			}else{
				sb.append("No");
			}
			vo.setReferAttorneyAcctept(sb.toString());
			
			sb = new StringBuilder();
			sb.append("Date Accepted: ");
			if("1".equals(followup.getReferAttorneyAccteptFlag())){
				String accteptDate= DateUtil.dateTOString(followup.getReferAttorneyAccteptDate(), DateUtil.MM_DD_YYYY);
				if(StringUtils.isNotBlank(accteptDate)){
					sb.append(accteptDate);
				}
			}
			vo.setAccteptDate(sb.toString());
			
			sb = new StringBuilder();
			sb.append("Reason, if given: ");
			if(!"1".equals(followup.getReferAttorneyAccteptFlag())){
				sb.append(followup.getReferAttorneyDeclinedReason());
			}
			vo.setDeclinedReason(sb.toString());
			
			sb = new StringBuilder();
			sb.append("Notes on Attorney Referral: ");
			if(StringUtils.isNotBlank(followup.getNotesOnReferal())){
				sb.append(followup.getNotesOnReferal());
			}
			vo.setNotesOnReferal(sb.toString());
		}else{
			sb.append("No");
			vo.setReferAttorneyStatus(sb.toString());
		}
	}
	
	protected void setAttorneyStatus(CaseFollowUp followup,FollowUpPdfVO vo){
		StringBuilder sb = new StringBuilder();
		sb.append("Attorney Status: ");
		String[] attorneyStatusArray=CaseService.CONTATC_WAY;
		if(attorneyStatusArray!=null){
			String attorneyStatus=followup.getAttorneyStatus();
			if(StringUtils.isNotBlank(attorneyStatus)){
				for(String status:attorneyStatusArray){
					if(attorneyStatus.contains(status)){
						sb.append(status).append(",");
					}
				}
			}
		}
		if(sb.lastIndexOf(",")>0 ){
			vo.setAttorneyStatus(sb.substring(0, sb.lastIndexOf(",")));
		}else{
			vo.setAttorneyStatus(sb.toString());
		}
		
		vo.setNotesOnAttorneyStatus(followup.getNotesOnAttorneyStatus());
	}
	
	protected void setContactInfo(CaseFollowUp followup,FollowUpPdfVO vo){
		if("1".equalsIgnoreCase(followup.getBeenContacted())){
			vo.setBeenContacted("Yes");
			
			StringBuilder sb = new StringBuilder();
			sb.append("Date Contacted: ");
			String contactDate=DateUtil.dateTOString(followup.getContactedDate(), DateUtil.MM_DD_YYYY);
			if(StringUtils.isNotBlank(contactDate)){
				sb.append(contactDate);	
			}
			vo.setContactedDate(sb.toString());
			
			sb = new StringBuilder();
			sb.append("By Who? ");
			if(StringUtils.isNotBlank(followup.getFollowedType())){
				if("0".equalsIgnoreCase(followup.getFollowedType())){
					sb.append("CCHR International, ");
					sb.append(followup.getStaffMemeberFirstName1());
					sb.append(followup.getStaffMemeberLastName1());
				}else if("1".equalsIgnoreCase(followup.getFollowedType())){
					sb.append("CCHR International, ");
					sb.append(followup.getStaffMemeberFirstName2());
					sb.append(followup.getStaffMemeberLastName2());
				}
			}
			vo.setFollowedType(sb.toString());
			
			sb = new StringBuilder();
			sb.append("Contacted by: ");
			String[] contactWays=CaseService.CONTATC_WAY;
			if(contactWays!=null && null != followup.getContactedBy()){
				String followUpBy=followup.getContactedBy();
				for(String contact:contactWays){
					if(followUpBy.contains(contact)){
						sb.append(contact).append(",");
					}
				}
			}
			if(sb.lastIndexOf(",")>0 ){
				vo.setContactedBy(sb.substring(0, sb.lastIndexOf(",")));
			}
			
			vo.setContactedNotes("<br>"+"Notes: "+followup.getContactedNotes());					
		}else{
			vo.setBeenContacted("No");
		}
	}
	
	protected void setComplaintInfo(CaseFollowUp followup,FollowUpPdfVO vo){
		if("1".equalsIgnoreCase(followup.getFilledWithAgency())){
			vo.setFilledWithAgency("Yes");
			
			StringBuilder sb = new StringBuilder();
			sb.append("Date Field: ");
			String agencyDate= DateUtil.dateTOString(followup.getFilleWithAgencyDate(), DateUtil.MM_DD_YYYY);
			if(StringUtils.isNotBlank(agencyDate)){
				sb.append(agencyDate);
			}
			vo.setFilleWithAgencyDate(sb.toString());
			
			sb = new StringBuilder();
			sb.append("Agency the Complaint Was Filed With: ");
			if(StringUtils.isNotBlank(followup.getFilledAgencyName())){
				sb.append(followup.getFilledAgencyName());
			}
			vo.setFilledAgencyName(sb.toString());
			
			sb = new StringBuilder();
			sb.append("Contact Name: ");
			if(StringUtils.isNotBlank(followup.getFilledAgencyContactName())){
				sb.append(followup.getFilledAgencyContactName());
			}
			vo.setFilledAgencyContactName(sb.toString());
			
			sb = new StringBuilder();
			sb.append("Address: ");
			if(StringUtils.isNotBlank(followup.getFilledAgencyAddress())){
				sb.append(followup.getFilledAgencyAddress()).append(", ");
			}
			if(StringUtils.isNotBlank(followup.getFilledAgencyCity())){
				sb.append(followup.getFilledAgencyCity()).append(", ");
			}
			if(StringUtils.isNotBlank(followup.getFilledAgencyState())){
				sb.append(followup.getFilledAgencyState()).append(" ");
			}
			if(StringUtils.isNotBlank(followup.getFilledAgencyZip())){
				sb.append(followup.getFilledAgencyZip()).append(", ");
			}
			sb.append(followup.getFilledAgencyCountry());
			vo.setAgencyAddr(sb.toString());
			
			
		}else{
			vo.setFilledWithAgency("No");
		}
	}
	
	protected void setResponseInfo(CaseFollowUp followup,FollowUpPdfVO vo){
		if("1".equalsIgnoreCase(followup.getCompliantResponseReceived())){
			vo.setResponseReceived("Yes");
			
			StringBuilder sb = new StringBuilder();
			sb.append("What Was the Response? ");
			if(StringUtils.isNotBlank(followup.getCompliantResponseContent())){
				sb.append(followup.getCompliantResponseContent());
			}
			vo.setResponseContent(sb.toString());
			
			sb = new StringBuilder();
			sb.append("Notes on Complaint Filed? ");
			if(StringUtils.isNotBlank(followup.getNoteOnCompliant())){
				sb.append(followup.getNoteOnCompliant());
			}
			vo.setNoteOnCompliant(sb.toString());
		}else{
			vo.setResponseReceived("No");
		}
	}
	
	protected void setSigneInfo(CaseFollowUp followup,FollowUpPdfVO vo){
		if("1".equalsIgnoreCase(followup.getAbusedPersonSigneWaiver())){
			vo.setPersonSigneWaiver("Yes");
			
			StringBuilder sb = new StringBuilder();
			sb.append("Date Signed: ");
			String date= DateUtil.dateTOString(followup.getAbusedPersonSigneDate(), DateUtil.MM_DD_YYYY);
			if(StringUtils.isNotBlank(date)){
				sb.append(date);
			}
			vo.setPersonSigneDate(sb.toString());
		}else{
			vo.setPersonSigneWaiver("NO");
		}
	}
	
	protected void setReceivedDoc(CaseFollowUp followup,FollowUpPdfVO vo){
		if("1".equalsIgnoreCase(followup.getCchrReceivedAbusedDoc())){
			vo.setReceivedAbusedDoc("Yes");
			
			StringBuilder sb = new StringBuilder();
			sb.append("Date Received: ");
			String date= DateUtil.dateTOString(followup.getCchrReceivedAbusedDocDate(), DateUtil.MM_DD_YYYY);
			if(StringUtils.isNotBlank(date)){
				sb.append(date);
			}
			vo.setReceivedAbusedDocDate(sb.toString());
		}else{
			vo.setReceivedAbusedDoc("No");
		}
	}
	
	protected void download(String filePath,HttpServletRequest request, HttpServletResponse response)  {
	    try {
	    	// path是指欲下载的文件的路径。
	        File file = new File(filePath);
	        // 取得文件名。
	        String fileName = file.getName();
	        // 取得文件的后缀名。
	        String ext = fileName.substring(fileName.lastIndexOf(".") + 1).toUpperCase();
	
	        // 以流的形式下载文件。
	        InputStream fis = new BufferedInputStream(new FileInputStream(filePath));
	        byte[] buffer = new byte[fis.available()];
	        fis.read(buffer);
	        fis.close();
	        // 清空response
	        response.reset();
	        // 设置response的Header
	        String agent = request.getHeader("USER-AGENT"); 
			String encodeFileName=null;
			if (null != agent && -1 != agent.indexOf("MSIE") || null != agent  
	                && -1 != agent.indexOf("Trident")) {// ie  
				encodeFileName = URLEncoder.encode(fileName, "UTF-8"); 
	        } else if (null != agent && -1 != agent.indexOf("Mozilla")) {// 火狐,chrome等  
	        	encodeFileName = new String(fileName.getBytes("utf-8"), "ISO8859-1");  
	        } 
			
			response.reset();
	        response.setContentType("application/octet-stream; charset=utf-8");
	        response.setHeader("Content-Disposition", "attachment; filename="+encodeFileName);
	        
	        OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
	        toClient.write(buffer);
	        toClient.flush();
	        toClient.close();
	    } catch (IOException ex) {
	        ex.printStackTrace();
	    }
	}
}
